﻿(function() {

	var SalesMakeover = {};
	
	SalesMakeover.ForgottenPassword = function()
	{
		var info=getText("forgottenText");
		var exit=getText("Exit");
		var send=getText("Send");

		var gab = document.createElement('div');
		gab.setAttribute('class', 'OVER');
		gab.setAttribute('style', 'display:block');
		gab.innerHTML=
		'<div class="overlayForgottenPassword" id="form_editEmployee"><p class="overlay_text"><b>Glömt lösenord</b></p>' +
		'<p class="overlay_text2">'+info+'</p>' +

		
		'<form id="forgottenPassword" method="post" action="forgotten.php" accept-charset="UTF-8">'+
		'<input type="text" id="email" name="email" onfocus="if(this.value == \'E-post\') { this.value = \'\'; }" onblur="if(this.value == \'\') { this.value = \'E-post\'; }" value="E-post">'+
		'<div class="form_add_buttons" id="button">'+
		'<div class="form_add_left_align" onclick="javascript:location.href=\'index.php\'">'+
		'<input type="button" class="button_black" value=' + exit + '>' +
		'</div>'+
		'<div class="form_add_right_align" id="button">'+
		'<input type="submit" class="button" value='+send+'>'+
		'</div></div></form></div>';

				
		
		
		$(".login_container").append(gab);
	};




	SalesMakeover.ChangeLang = function(lang)
	{
		var dataString = 'lang=' + lang;

		$.ajax({

		    type: "POST",
		    url: "changeLang.php",
		    data: dataString,
		    cache: false,
		    success: function(response) {
			    location.reload();
			}
		});

		return false;
	};

	SalesMakeover.validateAddForm = function() {
		var companynameVal = document.forms["form_add"]["companyname"].value;
		if(companynameVal == "")
		{
			alert(getText("Company_Name_Not_Empty"));
			return false;
		}	
		var companyemailVal = document.forms["form_add"]["companyemail"].value;
		if(companyemailVal == "")
		{
			alert(getText("Company_Email_Not_Empty"));
			return false;
		}
		var managernameVal = document.forms["form_add"]["managername"].value;
		if(managernameVal == "")
		{
			alert(getText("Manager_Name_Not_Empty"));
			return false;
		}
		var manageremailVal = document.forms["form_add"]["manageremail"].value;
		if(manageremailVal == "")
		{
			alert(getText("Manager_Email_Not_Empty"));
			return false;
		}
		var Taken = false;
		var Message = "";
		var companyname = $("input[name='companyname']").val();
		var companyemail = $("input[name='companyemail']").val();
		var manageremail = $("input[name='manageremail']").val();
		var formdata = 'cn=' + companyname + '&ce=' + companyemail + '&me=' + manageremail;
		$.ajax({
			url: "validation.php",
			type: 'POST',
			data: formdata,
			dataType: 'json',
			async: false,
			success: function(data)
			{
				Taken = data.result;
				Message = data.message;
			}
		});
		if(Message != "")
		{
			alert(getText(Message));
		}
		return Taken;
		
	};
	
	SalesMakeover.validateEditEmployeeForm = function(id){

		var employeenameVal = $("input[name='employeename']").val();
		if(employeenameVal == "")
		{
			alert(getText("Employee_Name_Not_Empty"));
			return false;
		}	
		var employeeloginVal = $("input[name='employeelogin']").val();
		if(employeeloginVal == "")
		{
			alert(getText("Employee_Email_Not_Empty"));
			return false;
		}
		var Taken = false;
		var Message = "";
		var employeeID = id;
		var formdata = 'eid=' + employeeID + '&ee=' + employeeloginVal;
		$.ajax({
			url: "validation.php",
			type: 'POST',
			data: formdata,
			dataType: 'json',
			async: false,
			success: function(data)
			{
				Taken = data.result;
				Message = data.message;
			},
			error: function(errorThrown)
			{
				alert(errorThrown.responseText);
			}
		});
		if(Message != "")
		{
			alert(getText(Message));
		}
		return Taken;

	};
	
	SalesMakeover.validateAddEmployeeForm = function() {

		var newemployeenameVal = $("input[name='newemployeename']").val();
		if(newemployeenameVal == "")
		{
			alert(getText("Employee_Name_Not_Empty"));
			return false;
		}	
		var newemployeeemailVal = $("input[name='newemployeeemail']").val();
		if(newemployeeemailVal == "")
		{
			alert(getText("Employee_Email_Not_Empty"));
			return false;
		}
		var supervisorIDVal = $("select[name='supervisorID']").val();
		if(supervisorIDVal == null)
		{
			alert(getText("Employee_Supervisor_Not_Empty"));
			return false;
		}
		var Taken = false;
		var Message = "";
		var employeemail = $("input[name='newemployeeemail']").val();
		var formdata = 'ee=' + employeemail;
		$.ajax({
			url: "validation.php",
			type: 'POST',
			data: formdata,
			dataType: 'json',
			async: false,
			success: function(data)
			{
				Taken = data.result;
				Message = data.message;
			},
			error: function(errorThrown)
			{
				alert(errorThrown.responseText);
			}
		});
		if(Message != "")
		{
			alert(getText(Message));
		}
		return Taken;

	};
	
	SalesMakeover.validateEditManagerForm = function(id){

		var managernameVal = $("input[name='managername']").val();
		if(managernameVal == "")
		{
			alert(getText("Manager_Name_Not_Empty"));
			return false;
		}	
		var manageremailVal = $("input[name='manageremail']").val();
		if(manageremailVal == "")
		{
			alert(getText("Manager_Email_Not_Empty"));
			return false;
		}
		var Taken = false;
		var Message = "";
		var managerID = id;
		var formdata = 'mid=' + managerID + '&me=' + manageremailVal;
		$.ajax({
			url: "validation.php",
			type: 'POST',
			data: formdata,
			dataType: 'json',
			async: false,
			success: function(data)
			{
				Taken = data.result;
				Message = data.message;
			},
			error: function(errorThrown)
			{
				alert(errorThrown.responseText);
			}
		});
		if(Message != "")
		{
			alert(getText(Message));
		}
		return Taken;

	};

	SalesMakeover.validateFormNewManager = function(){

		var newmanagernameVal = $("input[name='newmanagername']").val();
		if(newmanagernameVal == "")
		{
			alert(getText("Manager_Name_Not_Empty"));
			return false;
		}	
		var newmanageremailVal = $("input[name='newmanageremail']").val();
		if(newmanageremailVal == "")
		{
			alert(getText("Manager_Email_Not_Empty"));
			return false;
		}
		var Taken = false;
		var Message = "";
		var formdata = 'me=' + newmanageremailVal;
		$.ajax({
			url: "validation.php",
			type: 'POST',
			data: formdata,
			dataType: 'json',
			async: false,
			success: function(data)
			{
				Taken = data.result;
				Message = data.message;
			},
			error: function(errorThrown)
			{
				alert(errorThrown.responseText);
			}
		});
		if(Message != "")
		{
			alert(getText(Message));
		}
		return Taken;

	};

	SalesMakeover.validateFormSave = function(id){
		var Taken = false;
		var Message = "";
		var companyID = id;
		var companyemail = $("input[name='companyemail']").val();
		var formdata = 'cid=' + companyID + '&ce=' + companyemail;
		$.ajax({
			url: "validation.php",
			type: 'POST',
			data: formdata,
			dataType: 'json',
			async: false,
			success: function(data)
			{
				Taken = data.result;
				Message = data.message;
			},
			error: function(errorThrown)
			{
				alert(errorThrown.responseText);
			}
		});
		if(Message != "")
		{
			alert(getText(Message));
		}
		return Taken;

	};

	SalesMakeover.clickHidden = function(cb) {
		
		var val;
		if(cb.checked)
			val = 1;
		else
			val = 0;
			
		var  formData = 'h=' + val;
		$.ajax({
		url : "changeHidden.php",
		type: "POST",
		data : formData,
		success: function(data)
		{
			location.reload();
		}
		});
	}

	SalesMakeover.DeleteRow = function(id, name){
		if (confirm(getText("RemoveRow") + name + '?')) {
		    SalesMakeover.DBDeleteRow(id, name);
		    alert(name+getText("HaveRemoved"));
		}
	};

	SalesMakeover.DBDeleteRow = function(id, name){
		var  formData = 'ID=' + id;
		$.ajax({
			url : "deleteRow.php",
			type: "POST",
			data : formData,
			success: function(data)
			{
				location.reload();
				
			},
			error: function (jqXHR,textStatus,errorThrown)
			{
				alert(textStatus);
			}
		});
	};

	SalesMakeover.ReShowRow = function(id, name){
		if (confirm(getText("ReShowRow") + name + '?')) {
		    SalesMakeover.DBDeleteRow(id, name);
		    alert(name+getText("HaveShown"));
		}
	};

	var SendMailActive = true;
	SalesMakeover.SendMail = function(to, name, resetkey){
		if(!SendMailActive)
		{
			return;
		}
		SendMailActive = false;
		$(this)
		var  formData = 'to=' + to + "&name=" + name + "&resetkey=" + resetkey;
		alert(getText("NewMail")+name);
		$.ajax({
		    url : "sendNewUserInfoMail.php",
		    type: "POST",
		    data : formData,
		    
		    success: function(data)
		    {
			SendMailActive = true;
			
		    },
		    error: function (errorThrown)
		    {
			alert(errorThrown);
		    }
		});
		  
	};

	SalesMakeover.ChangeActive = function(id){
		var  formData = 'ID=' + id;
		$.ajax({
			url : "changeActive.php",
			type: "POST",
			data : formData,
			dataType: 'json',
			success: function(data)
			{
				location.reload();
			}
		});

	};

	SalesMakeover.saveCompany = function(id){
		var  formData = 'ID=' + id;
		$.ajax({
		    url : "saveCompany.php",
		    type: "POST",
		    data : formData,
		    success: function(data)
		    {
				
		    },
		    error: function (jqXHR,textStatus,errorThrown)
		    {
			alert(textStatus);
		    }
		});
	};

	SalesMakeover.hideDiv = function (obj,hideObj) {

	    var el = document.getElementById(obj);
	    if(hideObj)
	        el.style.display = 'none';
	    else
	        el.style.display = 'block';
	}


	SalesMakeover.doResize = function() {
		var h = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.documentElement.clientHeight) - 20;
		$('#tableHolder').css('max-height', (((h-300)*0.80)-100));
		$('#tableHolder').css('height', (((h-300)*0.80)-100));
	};
	
	SalesMakeover.doListUserResize = function() {
		var h = (typeof window.innerHeight != 'undefined' ? window.innerHeight : document.documentElement.clientHeight) - 20;
		$('#tableHolder').css('max-height', (((h-100)*0.80)-100));
		$('#tableHolder').css('height', (((h-100)*0.80)-100));
	};


	SalesMakeover.enableBeforeUnload = function() {
	    window.onbeforeunload = function (e) {
		return getText("ChangesDone");
	    };
	};
	SalesMakeover.disableBeforeUnload = function() {
	    window.onbeforeunload = null;
	};
	
	window.SalesMakeover = SalesMakeover;

}());